package com.zhong.gang.applib.utils;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.Application;
import android.app.Service;
import android.content.ComponentName;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DecodeFormat;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

public class GlideUtils {

    private static RequestOptions requestOptions;

    public static void initOption(int placePic,int errPic){
        requestOptions = new RequestOptions()
                .centerCrop()
                .dontAnimate()
                .format(DecodeFormat.PREFER_RGB_565)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(placePic)
                .error(errPic);
    }

    public static RequestOptions getRequestOptions(){
        if (requestOptions != null)
            return requestOptions;
        requestOptions = new RequestOptions()
                .frame(1000)
                .centerCrop()
                .dontAnimate()
                .format(DecodeFormat.PREFER_RGB_565)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        return requestOptions;
    }

    /**
     * 加载图片
     * @param context
     * @param object
     * @param imageView
     * @param placePic
     */
    public static void show(Context context, Object object, ImageView imageView,int placePic) {
        if (isContextExisted(context) && object != null && imageView != null ){
            Glide.with(context)
                    .load(object)
                    .thumbnail( 0.5f )
                    .apply(getOption(placePic,placePic))
//                    .transition(new DrawableTransitionOptions().crossFade(1000))
                    .into(imageView);
        }
    }


    public static void show(Context context, Object object, ImageView imageView,int placeholder,int errPic) {
        if (isContextExisted(context) && object != null && imageView != null ){
            Glide.with(context)
                    .load(object)
                    .thumbnail( 0.5f )
                    .apply(getOption(placeholder,errPic))
//                    .transition(new DrawableTransitionOptions().crossFade(1000))
                    .into(imageView);
        }
    }


    public static void show(Context context, Object object, ImageView imageView) {
        if (isContextExisted(context) && object != null && imageView != null){
            Glide.with(context)
                    .load(object)
                    .apply(getRequestOptions())
                    .thumbnail( 0.5f )
                    .into(imageView);
        }
    }



    public static RequestOptions getOption(int placePic,int errPic){
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .dontAnimate()
                .format(DecodeFormat.PREFER_RGB_565)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(placePic)
                .error(errPic);
        return options;
    }


    public static RequestOptions getOption(Drawable drawable, int errPic){
        RequestOptions options = new RequestOptions()
                .centerCrop()
                .dontAnimate()
                .format(DecodeFormat.PREFER_RGB_565)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .placeholder(drawable)
                .error(errPic);
        return options;
    }

    /**
     * 判断context是存货
     * @param context
     * @return
     */
    public static final boolean isContextExisted(Context context) {
        if (context != null) {
            if (context instanceof Activity) {
                if (!((Activity)context).isFinishing()) {
                    return true;
                }
            } else if (context instanceof Service) {
                if (isServiceExisted(context, context.getClass().getName())) {
                    return true;
                }
            } else if (context instanceof Application) {
                return true;
            }
        }
        return false;
    }

    public static final boolean isServiceExisted(Context context, String className) {
        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningServiceInfo> serviceList = activityManager
                .getRunningServices(Integer.MAX_VALUE);

        if (!(serviceList.size() > 0)) {
            return false;
        }
        for (int i = 0; i < serviceList.size(); i++) {
            ActivityManager.RunningServiceInfo serviceInfo = serviceList.get(i);
            ComponentName serviceName = serviceInfo.service;

            if (serviceName.getClassName().equals(className)) {
                return true;
            }
        }
        return false;
    }
}
